package com.oocl.crm.service.impl;

import com.oocl.crm.command.Command;
import com.oocl.crm.command.impl.OrderCommand;
import com.oocl.crm.exception.ParameterErrorException;
import com.oocl.crm.pojo.User;
import com.oocl.crm.service.BusinessService;

/**
 * O指令-业务处理-排序
 * @author GANAB
 *
 */
public class OrderBusinessService extends BusinessService{

	public String service(Command command) throws ParameterErrorException {
		OrderCommand orderCommand = (OrderCommand) command;
		if(orderCommand.getParams()!=null){
			if("id".equals(orderCommand.getParams())){
				return buildResult(this.getUserDB().sortById());
			}else if("name".equals(orderCommand.getParams())){
				return buildResult(this.getUserDB().sortByName());
			}else{
				throw new ParameterErrorException("Order user exception: not support this order! please try [id/name] ");
			}
		}
		throw new ParameterErrorException("Order user exception: parameter does not conform to the format! ");
	}
	
	private String buildResult(User[] userList){
		if(userList!=null){
			String resultStr = "";
			for(int i = 0, index=0; i < userList.length; i++){
				resultStr+="User data:["+(++index)+"]"+userList[i].toString()+"\n";
			}
			return resultStr;
		}
		return null;
	}

}
